from django.conf import settings
from django.core.files.storage import Storage
from django.utils.deconstruct import  deconstructible
from fdfs_client.client import  Fdfs_client

@deconstructible
class MyStorage(Storage):
    def __init__(self, base_url=None):
        if not base_url:
            base_url = settings.BASE_DIR
        self.base_url = base_url

    def open(self, name, mode='rb'):
        pass

    def save(self, name, content, max_length=None):

        #1.创建对象
        client = Fdfs_client('utils/fdfs/client.conf')

        #2.上传
        result = client.upload_by_buffer(content.read)
        #3.判断图片是否上传成功
        if result["status"] =="Upload successed":
            return result["Remote file_id"]
        else:
            raise Exception("图片")

    def exists(self, name):
        return False

    def url(self, name):

        return  self.base_url + name